package com.intexh.quickandroid.module.chat.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.intexh.quickandroid.R;
import com.intexh.quickandroid.base.BaseActivity;
import com.intexh.quickandroid.module.chat.event.UpdateChatMessageEvent;

import org.greenrobot.eventbus.EventBus;


/**
 * Created by Frank on 2017/4/25.
 * 聊天界面 没有其他界面交互用activity 否则用fragment
 */

public class AppChatActivity extends BaseActivity {

    public static final String UID = "uid";
    public static final String SESSION_ID = "session_id";
    public static final String IS_GROUP = "is_group";
    public static final String AVATAR = "avatar";
    public static final String GROUP_AVATAR = "chat_avatar";
    public static final String NICK_NAME = "nick_name";
    public static final String GROUP_NAME = "chat_title";
    public static final String GROUP_ID = "GROUP_ID";
    public static final String GENDER = "gender";

    //跳转群聊
    public static void startGroupActivity(Context context,String group_id,String groupSessionId,String groupName,String groupAvatar){
        Intent intent = new Intent(context,AppChatActivity.class);
        intent.putExtra(SESSION_ID, groupSessionId);
        intent.putExtra(IS_GROUP, true);
        intent.putExtra(GROUP_ID,group_id);
        intent.putExtra(GROUP_AVATAR,groupAvatar);
        intent.putExtra(GROUP_NAME,groupName);
        context.startActivity(intent);
    }

    //跳转单聊
    public static void startActivity(Context context, String uid, String sessionId, String avatar, String nickName){
        Intent intent = new Intent(context,AppChatActivity.class);
        intent.putExtra(UID,uid);
        intent.putExtra(SESSION_ID, sessionId);
        intent.putExtra(IS_GROUP, false);
        intent.putExtra(AVATAR,avatar);
        intent.putExtra(NICK_NAME,nickName);
        intent.putExtra(GENDER,0);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_chat);
        initExtra();
    }


    private void initExtra() {
        Intent intent = getIntent();
        Bundle bundle = new Bundle();
        String userId =intent.getStringExtra(UID);
        String sessionId = intent.getStringExtra(SESSION_ID);
        String avatar = intent.getStringExtra(AVATAR);
        String nickName = intent.getStringExtra(NICK_NAME);
        String groupId = intent.getStringExtra(GROUP_ID);
        String chatTitle = intent.getStringExtra(GROUP_NAME);
        String chatAvatar = intent.getStringExtra(GROUP_AVATAR);
        boolean isGroup = intent.getBooleanExtra(IS_GROUP,false);
        Fragment chatFragment;
        chatFragment = new AppChatFragment();
        bundle.putString(SESSION_ID,sessionId);
        bundle.putString(GROUP_ID,groupId);
        bundle.putString(UID,userId);
        bundle.putString(AVATAR,avatar);
        bundle.putString(GROUP_AVATAR,chatAvatar);
        bundle.putString(NICK_NAME,nickName);
        bundle.putString(GROUP_NAME,chatTitle);
        bundle.putBoolean(IS_GROUP,isGroup);
        chatFragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction().replace(com.intexh.quickandroid.R.id.root, chatFragment).commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //更新消息列表 或其他红点提示
        EventBus.getDefault().post(new UpdateChatMessageEvent(1));
    }

}
